package com.nmn.spinningwheel.network;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.nmn.spinningwheel.model.Ads;
import com.nmn.spinningwheel.model.Events;
import com.nmn.spinningwheel.model.Spin;
import com.nmn.spinningwheel.util.API;

import de.greenrobot.event.EventBus;

import android.content.Context;
import android.util.Log;

public class Helper {
	private AQuery aq;
	private EventBus eventBus;
	public Helper(Context context) {
		aq = new AQuery(context);
		eventBus=EventBus.getDefault();
	}
	public  boolean checkUserStatus(String str) {
		try {
			JSONObject jsonObject = new JSONObject(str);
			int error_code = jsonObject.getInt("error_code");
			if (error_code == 0 || error_code==60) {
				return true;
			} else {
				String error_message = jsonObject.getString("message");
				Log.e("Helper", "error user status" + error_message);
				return false;
			}

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	public  void getUserData(String access_token) {
		String url = API.GET_USER_DATA_URL + API.ACCESS_TOKEN + "="
				+ access_token + "&" + API.LANG + "=" + API.LANG_VALUE;
		Map<String, String> params = new HashMap<String, String>();
		params.put("version", String.valueOf(1));
		aq.ajax(url,params, JSONObject.class, new AjaxCallback<JSONObject>() {
            @Override
            public void callback(String url, JSONObject json, AjaxStatus ajaxStatus) {
                if(json != null){
                	if (checkUserStatus(json.toString())) {
            			JSONObject spinJsonObject; 
            			try {
            				JSONObject dataJsonObject = json.getJSONObject("data");
            				spinJsonObject = dataJsonObject.getJSONObject("spin");
            				JSONObject settingsObj=spinJsonObject.getJSONObject("settings");
            				JSONObject adsObj=settingsObj.getJSONObject("ads");
            				String description=adsObj.getString("description");
            				String image=adsObj.getString("image");
            				String type=adsObj.getString("type");
            				String store=adsObj.getString("store");
            				String uri=adsObj.getString("uri");
            				Ads ads=new Ads(description, image, type, store, uri);
            				JSONObject freeSpin = spinJsonObject
            						.getJSONObject("free_spin");
            				boolean freeSpinStatus=freeSpin.getBoolean("status");
            				int free_time_available=freeSpin.getInt("time_available");
            				
            				JSONObject silverSpin = spinJsonObject.getJSONObject("sivler_spin");
            				boolean silverSpinStatus=silverSpin.getBoolean("status");
            				int silver_spin_purple_tym=silverSpin.getInt("purple_tym_available");
            				int silver_spin_bet_purple=silverSpin.getInt("bet_purple_tym");
            				
            				JSONObject goldSpin = spinJsonObject.getJSONObject("gold_spin");
            				boolean goldSpinStatus=goldSpin.getBoolean("status");
            				int gold_spin_green_tym=goldSpin.getInt("green_tym_available");
            				int gold_spin_bet_green=goldSpin.getInt("bet_green_tym");
            				int gold_ticket=goldSpin.getInt("gold_ticket");
            				int yellow_tym=settingsObj.getInt("yellow_tym_available");
            				String gameToken = settingsObj.getString("game_token");
            				Spin spin=new Spin();
            				spin.setAds(ads);
            				spin.setFree_spin_status(freeSpinStatus);
            				spin.setFree_spin_time_available(free_time_available);
            				spin.setSilver_spin_status(silverSpinStatus);
            				spin.setSilver_spin_purple_tym(silver_spin_purple_tym);
            				spin.setSilver_spin_bet_purple_tym(silver_spin_bet_purple);
            				spin.setGold_spin_status(goldSpinStatus);
            				spin.setGold_spin_green_tym(gold_spin_green_tym);
            				spin.setGold_spin_bet_green_tym(gold_spin_bet_green);
            				spin.setGold_spin_gold_ticket(gold_ticket);
            				spin.setGame_token(gameToken);
            				spin.setYellow_tym(yellow_tym);
            				
            			} catch (JSONException e) {
            				e.printStackTrace();
            				
            			}
            		}
                } else {
                	Log.e("Helper", "Error: " + ajaxStatus.getCode());
                	eventBus.post(new Events.RequestFailEvent(ajaxStatus.getCode()));
                }
            }
		});
	}
}
